﻿Imports VendasDAL
Imports Conection
Public Class CarroBLL
    Dim ObjDAL As New CarroDAL
#Region "Propriedades"
    Property Codigo As Integer
    Property Descricao As String
    Property Status As Integer
    Property Modelo As String
    Property CodMarca As Integer
    Property MarcaDescricao As String
#End Region

#Region "Construtores"
    Sub New()

    End Sub
    Sub New(ByVal codigo As Integer)
        carrega(codigo)
    End Sub
    Sub New(codigo As Integer, descricao As String, status As Integer, modelo As String, codmarca As Integer)
        Me.Codigo = codigo
        Me.Descricao = descricao
        Me.Status = status
        Me.Modelo = modelo
        Me.CodMarca = codmarca
    End Sub

#End Region

#Region "Métodos"
#Region "Inserir"
    Function Inserir() As Boolean
        Try
            ObjDAL = New CarroDAL
            If (Trim(Descricao) <> "" And Modelo <> "" And CodMarca > 0) Then
                If (ObjDAL.ValidaReplica(0, Me.Descricao, Me.Modelo)) Then
                    Me.Codigo = ObjDAL.Inserir(Me.Descricao, Me.Status, Me.Modelo, Me.CodMarca)
                Else : Throw New Exception("Carro já cadastrado")
                End If
            Else : Throw New Exception("Favor preencher os campos obrigatórios")
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        If (Me.Codigo <= 0) Then
            Return False
        Else : Return True
        End If
    End Function

#End Region

#Region "Alterar"
    Function Alterar() As Boolean
        Dim ret As Integer
        Try
            ret = -1
            ObjDAL = New CarroDAL
            If (Descricao <> "") Then
                If (ObjDAL.ValidaReplica(Me.Codigo, Me.Descricao, Me.Modelo)) Then
                    If (ObjDAL.ValidaExcluir(Me.Codigo, Me.Status)) Then
                        ret = ObjDAL.Alterar(Me.Descricao, Me.Status, Me.Codigo, Me.Modelo, Me.CodMarca)
                    Else : Throw New Exception("Este carro não pode ser modificado, verifique antes a ocorrência do mesmo no(s) formulário(s): Placa")
                    End If
                Else : Throw New Exception("Carro já cadastrado")
                End If
            End If


        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        If (ret <= 0) Then
            Return False
        Else : Return True
        End If
    End Function
#End Region

#Region "Excluir"
    Function Excluir() As Boolean
        Dim ret As Integer
        Try
            ObjDAL = New CarroDAL
            If (ObjDAL.ValidaExcluir(Me.Codigo, Status)) Then
                ret = ObjDAL.Deletar(Codigo)
            Else : Throw New Exception("Este carro não pode ser modificado, verifique antes a ocorrência do mesmo no(s) formulário(s): Placa")
            End If

        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        If (ret = -1) Then
            Return False
        Else : Return True
        End If
    End Function

#End Region

#Region "Buscar"
    Function buscar(ByVal descricao As String, tipo As Integer) As DataTable
        Dim ret As DataTable
        Try
            ObjDAL = New CarroDAL
            ret = ObjDAL.buscar(descricao, tipo)
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
        Return ret
    End Function
#End Region
#End Region

#Region "Carrega Propriedades"
    Sub carrega(ByVal codigo As Integer)
        Dim tab As DataTable
        Try
            ObjDAL = New CarroDAL
            tab = ObjDAL.buscar(codigo, 0)
            If (tab.Rows.Count > 0) Then
                Me.Codigo = Integer.Parse(tab.Rows(0)("crr_codigo").ToString)
                Me.Descricao = tab.Rows(0)("crr_descricao").ToString
                Me.Status = tab.Rows(0)("sta_codigo").ToString
                Me.Modelo = tab.Rows(0)("crr_modelo").ToString
                Me.CodMarca = tab.Rows(0)("mar_codigo").ToString
                Me.MarcaDescricao = tab.Rows(0)("mar_descricao").ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            ObjDAL = Nothing
        End Try
    End Sub
#End Region

#Region "Limpa Propriedades"
    Sub limpar()
        Me.Codigo = Nothing
        Me.Descricao = Nothing
        Me.Status = Nothing
    End Sub
#End Region
End Class
